package com.event.controller;

import com.alibaba.fastjson.JSON;
import com.event.util.OssUtil;
import com.event.util.SenInfoCheckUtil;
import com.event.util.uuid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("/oss")
public class ossController {

    @Autowired
    OssUtil ossUtil;

    @PostMapping("/uploadfile")
    public JSON fileUpload(@RequestParam("file") MultipartFile file, String fileName) throws Exception {
        if (!SenInfoCheckUtil.imgFilter(file)){
            Map<String, Object> returnbody = new ConcurrentHashMap<>();
            returnbody.put("status","4444");
            returnbody.put("message","图片敏感");
            JSON json = (JSON) JSON.toJSON(returnbody);
            return  json;
        }
        try {
            String url = ossUtil.uploadFile(file,fileName+uuid.getUUID()); //调用OSS工具类
            Map<String, Object> returnbody = new ConcurrentHashMap<>();
            Map<String, Object> returnMap = new ConcurrentHashMap<>();
            returnMap.put("url", url);
            if (url.equals("error")){
                returnbody.put("status","500");
                returnbody.put("message","上传失败");
            }else {
                returnbody.put("data",returnMap);
                returnbody.put("status","200");
                returnbody.put("message","上传成功");
            }
            JSON json = (JSON) JSON.toJSON(returnbody);
            return json;
        }
        catch (Exception e) {
            Map<String, Object> returnbody = new ConcurrentHashMap<>();
            returnbody.put("data",null);
            returnbody.put("status","500");
            returnbody.put("message","上传失败");
            JSON json = (JSON) JSON.toJSON(returnbody);
            return  json;
        }
    }
    @PostMapping("/study")
    public JSON study(@RequestParam("file") MultipartFile file,String fileName) throws Exception {
        if (!SenInfoCheckUtil.imgFilter(file)){
            Map<String, Object> returnbody = new ConcurrentHashMap<>();
            returnbody.put("status","4444");
            returnbody.put("message","图片敏感");
            JSON json = (JSON) JSON.toJSON(returnbody);
            return  json;
        }
        try {
            String url = ossUtil.uploadFile(file,fileName); //调用OSS工具类
            Map<String, Object> returnbody = new ConcurrentHashMap<>();
            Map<String, Object> returnMap = new ConcurrentHashMap<>();
            returnMap.put("url", url);
            if (url.equals("error")){
                returnbody.put("status","500");
                returnbody.put("message","上传失败");
            }else {
                returnbody.put("data",returnMap);
                returnbody.put("status","200");
                returnbody.put("message","上传成功");
            }
            JSON json = (JSON) JSON.toJSON(returnbody);
            return json;
        }
        catch (Exception e) {
            Map<String, Object> returnbody = new ConcurrentHashMap<>();
            returnbody.put("data",null);
            returnbody.put("status","500");
            returnbody.put("message","上传失败");
            JSON json = (JSON) JSON.toJSON(returnbody);
            return  json;
        }
    }
}
